package com.ilovepostcard.controller.server;

import java.util.Date;
import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ilovepostcard.model.Postcard;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.JsonUtil;

public class TestUtil {

    private static User TEST_USER;
    
    public static User getTestUser() {
        if (TEST_USER == null) {
            User user = new User();
            user.UserID = 1L;
            user.Username = ("novajy@163.com");
            user.Nickname = ("Jerry");
            user.password = ("123456");
            user.Birthday = ("1985.06.02");
            user.HeadImgUrl = ("http://...");
            user.Introduction = ("This is Jerry!");
            user.MySpace = ("www.google.com");
            TEST_USER = user;
        }
        return TEST_USER;
    }
    
    public static String JSON_register_success() {
        User user = getTestUser();
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnUserRegisterResult\",");
        sb.append("\"result\": ").append(user.UserID).append(",");
        sb.append("\"errormsg\": ").append("\"\",");
        sb.append("\"data\": ").append("{}");
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_register_fail() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnUserRegisterResult\",");
        sb.append("\"result\": ").append(-1).append(",");
        sb.append("\"errormsg\": ").append("\"失败消息（测试用）\",");
        sb.append("\"data\": ").append("{}");
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_login_success() {
        User user = getTestUser();
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnUserLoginResult\",");
        sb.append("\"result\": ").append(user.UserID).append(",");
        sb.append("\"errormsg\": ").append("\"\",");
        sb.append("\"data\": ").append("{}");
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_login_fail() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnUserLoginResult\",");
        sb.append("\"result\": ").append(-1).append(",");
        sb.append("\"errormsg\": ").append("\"失败消息（测试用）\",");
        sb.append("\"data\": ").append("{}");
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_getuserinfo_success() {
        User user = randomUser();
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnGetUserInfoResult\",");
        sb.append("\"result\": ").append(1).append(",");
        sb.append("\"errormsg\": ").append("\"\",");
        sb.append("\"data\": ");
        {
            sb.append("{");
            sb.append("\"UserID\": ").append(user.UserID).append(",");
            sb.append("\"Username\": ").append("\"").append(user.Username).append("\",");
            sb.append("\"Nickname\": ").append("\"").append(user.Nickname).append("\",");
            sb.append("\"IsValidEmail\": ").append(user.IsValidEmail).append(",");
            sb.append("\"HasHeadImg\": ").append(user.HasHeadImg).append(",");
            sb.append("\"HeadImgUrl\": ").append("\"").append(user.HeadImgUrl).append("\",");
            sb.append("\"Status\": ").append(user.Status).append(",");
            sb.append("\"MyAttentionCount\": ").append(user.MyAttentionCount).append(",");
            sb.append("\"BeAttentionCount\": ").append(user.BeAttentionCount).append(",");
            sb.append("\"PublishCount\": ").append(user.PublishCount).append(",");
            sb.append("\"PlatformType\": ").append(user.PlatformType).append(",");
            sb.append("\"Introduction\": ").append("\"").append(user.Introduction).append("\",");
            sb.append("\"MySpace\": ").append("\"").append(user.MySpace).append("\",");
            sb.append("\"Sex\": ").append(user.Sex).append(",");
            sb.append("\"Birthday\": ").append("\"").append(user.Birthday).append("\",");
            sb.append("\"Province\": ").append("\"").append(user.Province).append("\",");
            sb.append("\"City\": ").append("\"").append(user.City).append("\"");
            sb.append("}");
        }
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_getuserinfo_fail() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnGetUserInfoResult\",");
        sb.append("\"result\": ").append(-1).append(",");
        sb.append("\"errormsg\": ").append("\"失败消息（测试用）\",");
        sb.append("\"data\": ");
        {
            sb.append("{");
            
            sb.append("}");
        }
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_getPostcard_success() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnGetMyPostcardResult\",");
        sb.append("\"result\": ").append(1).append(",");
        sb.append("\"errormsg\": ").append("\"\",");
        sb.append("\"data\": ");
        {
            sb.append("[");
            int testCount = randomInt(25);
            for (int i = 0; i < testCount; i++) {
                Postcard postcard = randomPostcard();
                try {
                    JSONObject json = JsonUtil.postcard2Json(postcard);
                    sb.append(json.toString());
                    if (i != testCount - 1)
                        sb.append(",");
                } catch(JSONException e) {
                    Log.e("postcard-test", "test error");
                }
            }
            sb.append("]");
        }
        sb.append("}");
        return sb.toString();
    }
    
    public static String JSON_getPostcard_fail() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"method\": ").append("\"OnGetMyPostcardResult\",");
        sb.append("\"result\": ").append(0).append(",");
        sb.append("\"errormsg\": ").append("\"失败消息（测试用）\",");
        sb.append("\"data\": ");
        {
            sb.append("[");
            sb.append("]");
        }
        sb.append("}");
        return sb.toString();
    }
    
    private static Random random = new Random(System.currentTimeMillis());
    
    private static int randomInt() {
//        Random random = new Random(System.currentTimeMillis());
        return random.nextInt(99999) + 1;
    }
    
    public static int randomInt(int max) {
//        Random random = new Random(System.currentTimeMillis());
        return random.nextInt(max) + 1;
    }
    
    private static boolean randomBoolean() {
//        Random random = new Random(System.currentTimeMillis());
        return random.nextBoolean();
    }
    
    private static String[] head_images = {
        "http://ww3.sinaimg.cn/mw220/65da57edgw1dvrouxol4wj.jpg",
        "http://ww3.sinaimg.cn/mw240/a1b3ef35gw1dxc6i6rrzwj.jpg",
        "http://ww1.sinaimg.cn/mw240/7ba5e40bjw1dxc75h9gn1j.jpg",
        "http://ww4.sinaimg.cn/mw240/ab9f0824gw1dxc1w3t6xsj.jpg",
        "http://ww3.sinaimg.cn/mw240/8d827ad6gw1dxc2nembe9j.jpg",
    };
    
    private static String[] postcard_images = {
        "http://ww4.sinaimg.cn/large/75047b24gw1dxbxe08fpdj.jpg",
        "http://ww2.sinaimg.cn/large/605b4f30gw1dwuscjoyjwj.jpg",
        "http://ww2.sinaimg.cn/large/605b4f30gw1dwuscth9d2j.jpg",
        "http://ww3.sinaimg.cn/large/5f035fadgw1dxa2pf94eej.jpg",
        "http://ww3.sinaimg.cn/large/5f035fadgw1dxa2pf94eej.jpg",
        "http://ww3.sinaimg.cn/large/3edf8b79gw1dxbz32bxd2j.jpg",
        "http://ww3.sinaimg.cn/large/6fec7949jw1dx9xwjy1ufj.jpg",
        "http://ww3.sinaimg.cn/large/75047b24gw1dxbxeagf16j.jpg",
    };
    
    private static String randomHeadImageUrl() {
        int length = head_images.length;
//        Random random = new Random(System.currentTimeMillis());
        int index = random.nextInt(length);
        return head_images[index];
    }
    
    private static String randomPostcardImageUrl() {
        int length = postcard_images.length;
//        Random random = new Random(System.currentTimeMillis());
        int index = random.nextInt(length);
        return postcard_images[index];
    }
    
    private static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    private static String randomString() {
//        Random random = new Random(System.currentTimeMillis());
        int lenght = random.nextInt(16) + 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lenght; i++) {
            sb.append(randomChar(random));
        }
        return sb.toString();
    }
    
    private static char randomChar(Random random) {
        int index = random.nextInt(LETTERS.length());
        return LETTERS.charAt(index);
    }
    
    public static User randomUser() {
        User user = new User();
        user.UserID = randomInt();         //�û�ID�����ͣ�Int64
        user.Username = randomString();       //�û������ͣ�String
        user.Nickname = randomString();       //�ǳƣ����ͣ�String
        user.IsValidEmail = randomBoolean();   //�����Ƿ�����֤�����ͣ�Boolean
        user.HasHeadImg = true;        //�Ƿ�ӵ��ͷ��(��ʶ�û��Ƿ��ϴ���ͷ��) �����ͣ�Boolean
        user.HeadImgUrl = randomHeadImageUrl();      //ͷ���ַ�������Ƿ�ӵ��ͷ��ͷ���ַ���᷵�أ������ͣ�String
        user.Status = randomInt(2);         //�û�״̬(1,��2,��) �����ͣ�Int32
        user.MyAttentionCount = randomInt();   //�ҹ�ע���������ͣ�Int32
        user.BeAttentionCount = randomInt();   //��ע�ҵ��������ͣ�Int32
        user.PublishCount = randomInt();       //����������Ƭ���������ͣ�Int32
        user.PlatformType = randomInt(2);    //����ƽ̨���ͣ�1=���ˣ�2=QQ
        user.Introduction = randomString(); //���ҽ��ܣ����ͣ�String
        user.MySpace = randomString();  //���˿ռ�URL�����ͣ�String
        user.Sex = randomInt(2); //�Ա����ͣ�Int32��0,δ֪��1,�У�2,Ů��
        user.Birthday = randomString(); //���գ�û����������ʱΪ���ַ������ͣ�String
        user.Province = randomString(); //ʡ��
        user.City = randomString();      //����
        user.password = randomString(); 
        return user;
    }
    
    private static Postcard randomPostcard() {
        Postcard postcard = new Postcard();
        postcard.PostcardID = (randomInt());
        postcard.Longitude = (randomInt());
        postcard.Latitude = (randomInt());
        postcard.ToFriendWords = (randomString());
        postcard.Story = (randomString());
        postcard.PublishTime = (new Date().toLocaleString());
        postcard.Likes = (randomInt());
        postcard.Comments = (randomInt());
        postcard.Favorite = (randomInt());
        postcard.IsShowWords = (randomBoolean());
        postcard.UserID = (randomInt());
        postcard.IsHorizontal = (randomBoolean());
        String postcardImage = randomPostcardImageUrl();
        postcard.ThumImgUrl = (postcardImage);
        postcard.PerviewImgUrl = (postcardImage);
        postcard.ViewPermission = randomInt(2) - 1;
        postcard.LocationPermission = randomInt(2) - 1;
        postcard.TagIn = randomString();
        return postcard;
    }
}
